//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    hiddens: true,
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    classifyProduct0: [],
    classifyProduct1: [],
    classifyProduct2: [],
    classifyProduct3: [],
    classifyProduct4: [],
    classifyProduct5: [],
    classifyProduct6: [],
    classifyBanner0: [],
    classifyBanner1: [],
    classifyBanner2: [],
    classifyBanner3: [],
    classifyBanner4: [],
    classifyBanner5: [],
    classifyBanner6: [],
    tips: ["物流现已停运，请各位初八再下单！","祝贺品德酒庄WinerySharing葡萄酒首达成都！",
      "成都国际铁路港首单跨境电商保税备货顺利出区！",
      "活动期间充值即送维诺卡夫葡萄酒柜！"],
    tabMenus: [{
      title: "全部商品",
      logo: "../../image/quanbu@2x.png",
      id: 1
    },
    {
      title: "品德共享",
      logo: "../../image/pindegongxiang@2x.png",
      id: 2
    },
    {
      title: "成为庄主",
      logo: "../../image/chengwei@2x.png",
      id: 3
    },
    {
      title: "余额",
      logo: "../../image/yue@2x.png",
      id: 4
    },
    ],
  },

  onLoad: function (options) {
    var that = this;
    //console.log(1111111111111111111111111);
    this.setData({
      imageUrl: app.globalData.imageUrl
    })
    getIndexBanner(that)
    app.showShareMenus();
    var scene = decodeURIComponent(options.scene);
    if (scene != 'undefined') {
      app.globalData.referrerId = scene;
      this.getUserInfo();
    }
    if (options.id) {
      app.globalData.referrerId = options.id;
      this.getUserInfo();
    } else {
      this.getUserInfo();
    }
  },
 /* getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },*/
  navMenus: function (e) {
    if (e.target.id == 1) {
      wx.navigateTo({
        url: '../../pages/allGoods/allGoods',
      })
    } else if (e.target.id == 4) {
      wx.navigateTo({
        url: '../../pages/page4/page4',
      })
    } else if (e.target.id == 3) {
      wx.navigateTo({
        url: '../../pages/becomeOwner/becomeOwner',
      })
    } else if (e.target.id == 2) {
      wx.navigateTo({
        url: '../../pages/aboutUs/aboutUs',
      })
    }
  }

  , onShow: function () {
    var that = this;
    if (wx.getStorageSync("isUpdatePrice") == 1) {
      getDirectProduct(this, 8);
      getDirectProduct(this, 9);
      getDirectProduct(this, 10);
      getDirectProduct(this, 11);
      getDirectProduct(this, 12);
      getDirectProduct(this, 13);
      getDirectProduct(this, 24);
      wx.setStorageSync("isUpdatePrice", 0)
    }
  },
  getUserInfo: function (cb) {//获取用户的唯一 openid
    var that = this;
    /*if (app.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {*/
      //调用登录接口
      wx.login({
        success: function (rese) {
          wx.getUserInfo({
            success: function (res) {
              app.globalData.hasRoot = false;
              app.globalData.noRoot = true;
              wx.setStorageSync('userInfo', res.userInfo)//将用户信息储存在本
              app.globalData.userInfo = res.userInfo;
              var reg = /[A-Za - z0 - 9_\-\u4e00 - \u9fa5]+/
              if (!reg.test(res.userInfo.nickName)) {
                res.userInfo.nickName == "默认用户名"
              }
              //console.log(app.globalData.referrerId);            
              wx.request({
                url: app.globalData.allUrl + "/utilAction/wxLogin",
                data: {
                  code: rese.code,
                  username: res.userInfo.nickName,
                  headportrait: res.userInfo.avatarUrl,
                  referrerId: app.globalData.referrerId,
                },
                method: 'GET',
                success: function (res) {
                  //console.log(res.data)
                  var userTypeName;
                  app.globalData.referrerId = res.data;
                  wx.setStorageSync('userid', res.data.id);
                  wx.setStorageSync('userType', res.data.isMerchant);
                  wx.setStorageSync('userAgentId', res.data.agentId);
                  wx.setStorageSync('remoteUserDetail', res.data);
                  wx.setStorageSync("userBalance", res.data.balance);
                  wx.setStorageSync("agentId", res.data.agentId);
                  wx.setStorageSync("integral", res.data.integral);//积分                  
                  if (res.data.isMerchant == 1) {
                    userTypeName = "体验庄主"
                  } else if (res.data.isMerchant == 2) {
                    userTypeName = "禾怡庄主"
                  } else if (res.data.isMerchant == 3) {
                    userTypeName = "古堡庄主"
                  } else if (res.data.isMerchant == 4) {
                    userTypeName = "金钥匙庄主"
                  } else if (res.data.isMerchant == 5) {
                    userTypeName = "管理员庄主"
                  }
                  wx.setStorageSync('userTypeName', userTypeName);
                  getDirectProduct(that, 8);
                  getDirectProduct(that, 9);
                  getDirectProduct(that, 10);
                  getDirectProduct(that, 11);
                  getDirectProduct(that, 12);
                  getDirectProduct(that, 13);
                  getDirectProduct(that, 24);
                }, fail: function () {
                }
              })
            }, fail: function () {
              app.globalData.hasRoot = true;
              app.globalData.noRoot = false;
            }
          })
        }
      })
   // }
  },
  onShareAppMessage: function (res) {
    return {
      title: '品德共享酒庄',
      path: '/pages/index/index?id=' + wx.getStorageSync("userid"),
    }
  },
})


function getDirectProduct(that, id) {
  var _classifyBanner = [];
  wx.request({
    url: app.globalData.allUrl + "/directAction/getDirectProduct",
    data: {
      directId: 1,
      pageNum: 1,
      pageCount: 30,
      classify: id,
      putaway: 1
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      //  console.log(res)
      for (var a = 0; a < res.data.list.length; a++) {
        res.data.list[a].sprice2 = parseFloat(res.data.list[a].price2 * 0.21).toFixed(2);
        res.data.list[a].sprice3 = parseFloat(res.data.list[a].price3 * 0.21).toFixed(2);
        res.data.list[a].sprice4 = parseFloat(res.data.list[a].price4 * 0.21).toFixed(2);
        res.data.list[a].sprice5 = parseFloat(res.data.list[a].price5 * 0.21).toFixed(2);
        res.data.list[a].sprice6 = parseFloat(res.data.list[a].price6 * 0.21).toFixed(2);
        if (wx.getStorageSync("userType") == 1) {
          res.data.list[a].disprice = res.data.list[a].price2
          res.data.list[a].dissprice = res.data.list[a].sprice2
        } else if (wx.getStorageSync("userType") == 2) {
          res.data.list[a].disprice = res.data.list[a].price3
          res.data.list[a].dissprice = res.data.list[a].sprice3
        } else if (wx.getStorageSync("userType") == 3) {
          res.data.list[a].disprice = res.data.list[a].price4
          res.data.list[a].dissprice = res.data.list[a].sprice4
        } else if (wx.getStorageSync("userType") == 4) {
          res.data.list[a].disprice = res.data.list[a].price5
          res.data.list[a].dissprice = res.data.list[a].sprice5
        } else if (wx.getStorageSync("userType") == 5) {
          res.data.list[a].disprice = res.data.list[a].price6
          res.data.list[a].dissprice = res.data.list[a].sprice6
        } else {
          res.data.list[a].disprice = res.data.list[a].price2
          res.data.list[a].dissprice = res.data.list[a].sprice2
        }
        if (res.data.list[a].specification13 == "2") {
          _classifyBanner.push(res.data.list[a].adImg);
        }
      }
      if (id == 8) {
        that.setData({
          classifyProduct0: res.data,
          classifyBanner0: _classifyBanner,
        })
      } else if (id == 9) {
        that.setData({
          classifyProduct1: res.data,
          classifyBanner1: _classifyBanner,
        })
      } else if (id == 10) {
        that.setData({
          classifyProduct2: res.data,
          classifyBanner2: _classifyBanner,
        })
      } else if (id == 11) {
        that.setData({
          classifyProduct3: res.data,
          classifyBanner3: _classifyBanner,
        })
      } else if (id == 12) {
        that.setData({
          classifyProduct4: res.data,
          classifyBanner4: _classifyBanner,
        })
      } else if (id == 13) {
        that.setData({
          classifyProduct5: res.data,
          classifyBanner5: _classifyBanner,
        })
      } else if (id == 24) {
        that.setData({
          classifyProduct6: res.data,
          classifyBanner6: _classifyBanner,
        })

      }
      setLists(that);
    }
  })
}

function setLists(that) {
  that.setData({
    wineList: [
      {
        name1: "红葡萄酒",
        name2: "Red Wine",
        adimage: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        logo: "../../image/RedWine@2x.png",
        products: that.data.classifyProduct0,
        classifyBanner: that.data.classifyBanner0,
        id: 8
      },
      {
        name1: "白葡萄酒 ",
        name2: "White Wine",
        adimage: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        logo: "../../image/WhiteWine@2x.png",
        products: that.data.classifyProduct1,
        classifyBanner: that.data.classifyBanner1
      },
      {
        name1: "桃红葡萄酒 ",
        name2: "Rosé Wine",
        adimage: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        logo: "../../image/Peachredwine@2x.png",
        products: that.data.classifyProduct2,
        classifyBanner: that.data.classifyBanner2
      },
      {
        name1: "起泡酒 ",
        name2: "Sparkling Wine",
        adimage: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        logo: "../../image/SparklingWine@2x.png",
        products: that.data.classifyProduct3,
        classifyBanner: that.data.classifyBanner3
      },
      {
        name1: "特别酿造 ",
        name2: "Specially brewed",
        adimage: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        logo: "../../image/Speciallybrewed@2x.png",
        products: that.data.classifyProduct4,
        classifyBanner: that.data.classifyBanner4
      },
      {
        name1: "儿童葡萄汁 ",
        name2: "Kids Bio Grape Juice",
        adimage: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        logo: "../../image/Kids@2x.png",
        products: that.data.classifyProduct5,
        classifyBanner: that.data.classifyBanner5
      },
      {
        name1: "其他 ",
        name2: "Others",
        adimage: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        logo: "",
        products: that.data.classifyProduct6,
        classifyBanner: that.data.classifyBanner6
      },
    ]
  })
}

function getIndexBanner(that) {
  wx.request({
    url: app.globalData.allUrl + "/utilAction/selectIndexBanner",
    data: {
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
     // console.log(res.data)
      that.setData({
        imgUrls: res.data
      })
    }
  })
}